using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class CustomerBillingUpdate : SSLPageBaseClass
    {

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            Website = WebsiteData.WebsiteDetail();
            PageID = Website.BillingUpdateTemplatePageID.Value;
            base.Page_PreInit(sender, e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Page.IsPostBack) return;

            CountryDropDownList.DataSource = WebsiteData.LocationCountryList();
            CountryDropDownList.DataBind();
            CountryDropDownList.SelectedValue = "US";

            PopulateStateDropDown();

            ShowPasswordRow(false);

            if (Customer != null)
                if (!String.IsNullOrEmpty(Customer.BillingAddressID))
                    LoadCustomerBillingInfo();

            if (!Website.AllowDuplicateEmailAddresses.Value && !isLoggedIn)
                ShowPasswordRow(true);

        }

        public void ShowPasswordRow(bool isVisible)
        {
            if (isVisible == true)
            {
                PasswordRow.Visible = true;
                ConfirmPasswordRow.Visible = true;
                PasswordMessageRow.Visible = false;
                ChangePasswordRow.Visible = false;
            }
            else
            {
                PasswordRow.Visible = false;
                ConfirmPasswordRow.Visible = false;
                if (!isLoggedIn)
                {
                    PasswordMessageRow.Visible = true;
                    ChangePasswordRow.Visible = false;
                }
                else
                {
                    PasswordMessageRow.Visible = false;
                    ChangePasswordRow.Visible = true;
                }
            }

        }

        public void LoadCustomerBillingInfo()
        {

            CountryDropDownList.SelectedValue = Customer.BillingAddress.AddressCountryIDPostalAbrev;
            PopulateStateDropDown();

            if (!String.IsNullOrEmpty(Customer.BillingAddress.AddressStateProvIDPostalAbrev))
                BillStateProvIDCodeDropDown.SelectedValue = Customer.BillingAddress.AddressStateProvIDPostalAbrev;

            BillCompanyNameTextBox.Text = Customer.BillingAddress.AddressCompanyName;
            BillFirstNameTextBox.Text = Customer.BillingAddress.AddressFirstName;
            BillLastNameTextBox.Text = Customer.BillingAddress.AddressLastName;
            BillAddress1TextBox.Text = Customer.BillingAddress.AddressLine1;
            BillAddress2TextBox.Text = Customer.BillingAddress.AddressLine2;
            BillCityTextBox.Text = Customer.BillingAddress.AddressCity;
            BillPostalCodeTextBox.Text = Customer.BillingAddress.AddressPostalCode;
            BillPhonePrimaryTextBox.Text = Customer.BillingAddress.AddressPhone;
            UserNameEmailTextBox.Text = Customer.UserNameEmail;

        }

        protected void ExposePasswordButton_Click(Object sender, System.EventArgs e)
        {
            ShowPasswordRow(true);
        }

        protected void UpdateCustomerInfoButton_Click(Object sender, EventArgs e)
        {

            string Password = null;
            if (PasswordTextBox.Text.Trim() != "")
                Password = PasswordTextBox.Text.Trim();
            if (PasswordTextBox.Text.Trim() == "" && isLoggedIn == false)
                Password = System.Guid.NewGuid().ToString().Substring(0, 6);

            string CustomerID = null;
            if (Customer != null)
                CustomerID = Customer.CustomerID;

            string AddressStateProvIDPostalAbrev = null;
            if (StateRow.Visible)
                AddressStateProvIDPostalAbrev = BillStateProvIDCodeDropDown.SelectedValue;

            APIEnvelope env = Soveryn.Commerce.Shared.API.Call(CustomerAPIURL, "CustomerAddUpdate", new
            {
                WebsiteAPIKey = PageBaseClass.WebsiteAPIKey,
                CustomerCartSessionID = CustomerCartSessionID,
                CustomerID = CustomerID,
                UserNameEmail = UserNameEmailTextBox.Text.Trim().ToLower(),
                Password = Password,
                CustOrigination = Website.WebsiteName,
                AddressCompanyName = BillCompanyNameTextBox.Text,
                AddressFirstName = BillFirstNameTextBox.Text,
                AddressLastName = BillLastNameTextBox.Text,
                AddressLine1 = BillAddress1TextBox.Text,
                AddressLine2 = BillAddress2TextBox.Text,
                AddressCity = BillCityTextBox.Text,
                AddressPhone = BillPhonePrimaryTextBox.Text,
                AddressPostalCode = BillPostalCodeTextBox.Text,
                AddressCountryIDPostalAbrev = CountryDropDownList.SelectedValue,
                AddressStateProvIDPostalAbrev = AddressStateProvIDPostalAbrev,
                AddressNickName = "Your Billing Address",
                IsCreditApproved = false,
                IsEmailOptIn = false,
                IsStateProvJurisTaxExempt = false,
                IsInactive = false,
                useBillingAddressAsShippingAddress = ShipToBillingAddressCheckbox.Checked
            }, false, false);

            Response.Redirect("/OrderCheckoutPROC.aspx", true);

        }

        protected void PopulateStateDropDown()
        {

            BillStateProvIDCodeDropDown.Items.Clear();
            BillStateProvIDCodeDropDown.DataSource = WebsiteData.LocationStateProvList(CountryDropDownList.SelectedValue);
            BillStateProvIDCodeDropDown.DataBind();
            BillStateProvIDCodeDropDown.Items.Insert(0, new ListItem("Select State...", "-1"));

            if (BillStateProvIDCodeDropDown.Items.Count == 1)
                StateRow.Visible = false;
            else
                StateRow.Visible = true;

        }

        protected void CountryDropDownList_SelectedIndexChanged(Object sender, System.EventArgs e)
        {

            PopulateStateDropDown();

        }

    }

}